package capricornus.res
{
	import capricornus.events.ResLoaderEvent;
	import capricornus.net.loader.ResLoader;
	import capricornus.utils.MathUtils;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	/**
	 * swf显示器，无缓存。
	 * 加载完成后会对外发送Event.COMPLETE消息
	 * @author kael
	 * 
	 */	
	[Event(name="complete", type="flash.events.Event")]
	public class SwfDisplayer extends Sprite
	{
		/**
		 * 适应类型，原样不做缩放，偏移等 
		 */		
		public static const NONE:int = 0;
		/**
		 * 适应类型，完全按照setSize大小显示 
		 */		
		public static const EXACT_FIT:int = 1;
		/**
		 * 适应类型，按照setSize的大小居中显示
		 */
		public static const CENTER:int = 2;
		
		/**
		 * loader
		 */		
		private var _loader:ResLoader;
		
		private var _hasBackground:Boolean = false;
		private var _back:Shape;
		
		/**
		 * 加载内容 
		 */		
		private var _content:DisplayObject;
		
		/**
		 * 适应类型 
		 */		
		private var _fitType:int = 0;
		
		/**
		 * 宽度 
		 */		
		private var _w:int = 1;
		
		/**
		 * 高度 
		 */		
		private var _h:int = 1;
		
		private var _url:String;
		
		/**
		 * swf显示器
		 * @param fitType 适应类型，默认为0 
		 * @param hasBackground 是否有透明背景，默认为false
		 * 
		 */		
		public function SwfDisplayer( fitType:int=NONE, hasBackground:Boolean=false )
		{
			super();
			_hasBackground = hasBackground;
			_fitType = fitType;
			init();
		}
		
		private function init():void
		{
			if( _hasBackground )
			{
				_back = new Shape();
				_back.graphics.beginFill( 0,0 );
				_back.graphics.drawRect( 0, 0, 1, 1 );
				_back.graphics.endFill();
				addChild( _back );
			}
		}
		
		public function destroy():void
		{
			releaseLastSoure();
		}
		
		public function clear():void
		{
			releaseLastSoure();
		}
		
		private var _hasSetSize:Boolean = false;
		public function setSize( w:int, h:int ):void
		{
			if( _back )
			{
				_back.width = w;
				_back.height = h;
			}
			_hasSetSize = true;
			_w = w;
			_h = h;
			resize();
		}
		
		override public function get width():Number
		{
			return _hasSetSize?_w:super.width;
		}
		
		override public function get height():Number
		{
			return _hasSetSize?_h:super.height;
		}
		
		public function setLocation( x:int, y:int ):void
		{
			this.x = x;
			this.y = y;
		}
		
		public function set source( value:String ):void
		{
			if( _url==value ) return;
			
			if( _url )
			{
				releaseLastSoure();
			}
			_url = value;
			if( _url )
			{
				if( stage )
				{
					load();
				}
				else addEventListener( Event.ADDED_TO_STAGE, onAddToStage, false, 0, true );
			}
		}
		
		private function load():void
		{
			_loader = new ResLoader();
			_loader.addEventListener( ResLoaderEvent.COMPLETE, onLoaded, false, 0, true );
			_loader.load( _url );
		}
		
		private function onAddToStage( e:Event ):void
		{
			removeEventListener( Event.ADDED_TO_STAGE, onAddToStage );
			if( _url ) load();
		}
		
		private function releaseLastSoure():void
		{
			if( _content )
			{
				removeChild( _content );
				_content = null;
			}
			if( _url )
			{
				if( _loader != null )
				{
					_loader.destroy();
					_loader.removeEventListener( ResLoaderEvent.COMPLETE, onLoaded );
					_loader = null;
				}
			}
			_url = null;
		}
		
		private function onLoaded( e:ResLoaderEvent ):void
		{
			_content = _loader.data;
			if( _content )
			{
				addChild( _content );
				resize();
			}
			_loader.destroy();
			_loader = null;
			
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
		
		private function resize():void
		{
			if( _content==null ) return;
			
			if( _fitType==EXACT_FIT )
			{
				_content.width = _w;
				_content.height = _h;
			}
			else if( _fitType==CENTER )
			{
				var arr:Array = MathUtils.scaleFit( _content.width, _content.height, _w, _h );
				_content.width = arr[0];
				_content.height = arr[1];
				_content.x = int((_w - _content.width)/2);
				_content.y = int((_h - _content.height)/2);
			}
		}
	}
}













